<?php
namespace DragonU\Core;
/**
 * CorePress Timer library.
 *
 * @internal
 * Copyright (c) 2009, Jacob Santos
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the DragonU nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @author Jacob Santos <wordpress@santosj.name>
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD license
 * @package CorePress
 * @subpackage DB
 * @since 0.1
 */

/**
 * CorePress Timer class.
 *
 * There are various places where timers are used to keep track of statistics of
 * when an event started and when it ended. The functionality should not be
 * duplicated over and over again.
 *
 * @author Jacob Santos
 * @since 0.1
 * @package CorePress
 * @subpackage Core
 */
class Timer
{

	/**
	 * Keep track of all of the elapses during each start and stop.
	 *
	 * @since 0.1
	 * @var array
	 */
	protected $log = array();

	/**
	 * Start time.
	 *
	 * @since 0.1
	 * @var double
	 */
	protected $startTime = null;

	/**
	 * Keep track of the total time.
	 *
	 * @since 0.1
	 * @var double
	 */
	protected $total = null;

	/**
	 * Setup static variables and connect to Database.
	 *
	 * @since 0.1
	 *
	 * @return CorePress\DB
	 */
	public function __construct()
	{
		$this->log = array();
		$this->startTime = null;
		$this->total = (double) 0.0;
	}

	/**
	 * Start timer.
	 *
	 * If start has already been called, then it will have no affect.
	 *
	 * @since 0.1
	 */
	public function start()
	{
		if( is_null( $this->startTime ) )
		{
			$this->startTime = (float) microtime(true);
			return true;
		}

		return false;
	}

	/**
	 * Stop timer.
	 *
	 * @since 0.1
	 * @return double Elapsed time between start and stop.
	 */
	public function stop()
	{
		$end = microtime(true);

		$elapse = 0.0;

		// @codeCoverageIgnoreStart
		if ( function_exists('bcsub') )
		{
			$elapse = (float) bcsub( $end, $this->startTime, 5 );
		}
		else if ( function_exists('gmp_sub') )
		{
			$elapse = (float) gmp_sub( $end, $this->startTime );
		}
		else
		{
			$elapse = round( ( (float) $end - (float) $this->startTime), 5 );
		}

		if ( function_exists('bcadd') )
		{
			$this->total = (float) bcadd($elapse, $this->total, 5);
		}
		else if ( function_exists('gmp_add') )
		{
			$this->total = (double) gmp_add( $elapse, $this->total );
		}
		else
		{
			$this->total += (double) $elapse;
		}
		// @codeCoverageIgnoreEnd

		$this->log[] = $elapse;
		$this->startTime = null;

		return (double) $elapse;
	}

	/**
	 * Retrieve total elapsed time.
	 *
	 * @since 0.1
	 *
	 * @return double
	 */
	public function total()
	{
		return (double) $this->total;
	}

	/**
	 * Retrieve log of elapsed times.
	 *
	 * @since 0.1
	 *
	 * @return array
	 */
	public function log()
	{
		return $this->log;
	}

}

